/**
 * Application name : Recipes App
 * Date				: March 2012
 */
package com.recipes.app;

import java.util.ArrayList;

import android.R.color;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.SQLException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;

public class RecipeDetail extends Activity {

	TextView txtRecipeName, txtPrepTime, txtCookTime, txtServes, txtSummary,
			txtIngredients, txtDirections, txtTitle;
	ImageView imgPreviewDetail;
	LinearLayout imgBack;
	ProgressBar prgLoading;
	ScrollView sclDetail;

	LinearLayout home;

	DBHelper dbhelper;
	ArrayList<Object> data;
	int id;
	String RecipeName, Preview_1, Preview_2, Preview_3, PrepTime, CookTime,
			Serves, Summary, Ingredients, Directions;

	private AlertDialog menuDialog = null;
	private PopupWindow mPopupWindow;

	String[] imageList;

	String title;
	String category;

	private static String SDPATH = "/mnt/sdcard";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recipe_detail);

		// SDPATH = Environment.getExternalStorageDirectory().getPath();
		Intent i_get = getIntent();
		id = i_get.getIntExtra("id_for_detail", 0);
		title = i_get.getStringExtra("title_for_detail");
		category = i_get.getStringExtra("category_for_detail");

		dbhelper = new DBHelper(this);

		txtRecipeName = (TextView) findViewById(R.id.txtRecipeName);
		txtPrepTime = (TextView) findViewById(R.id.txtPrepTime);
		txtCookTime = (TextView) findViewById(R.id.txtCookTime);
		txtServes = (TextView) findViewById(R.id.txtServes);
		txtSummary = (TextView) findViewById(R.id.txtSummary);
		txtIngredients = (TextView) findViewById(R.id.txtIngredients);
		txtDirections = (TextView) findViewById(R.id.txtDirections);
		imgPreviewDetail = (ImageView) findViewById(R.id.imgPreviewDetail);
		imgBack = (LinearLayout) findViewById(R.id.imgBack);
		prgLoading = (ProgressBar) findViewById(R.id.prgLoading);
		sclDetail = (ScrollView) findViewById(R.id.sclDetail);
		txtTitle = (TextView) findViewById(R.id.txtTitleApp);
		txtTitle.setText(title);

		home = (LinearLayout) findViewById(R.id.home);

		home.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent i = new Intent(RecipeDetail.this, CategoryList.class);
				i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(i);
				finish();
			}
		});

		imgBack.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub

				finish();
			}
		});

		try {
			dbhelper.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		}

		new getDetailTask().execute();

	}

	/** this class is used to handle thread */
	public class getDetailTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub

		}

		@Override
		protected Void doInBackground(Void... arg0) {
			// TODO Auto-generated method stub
			getDetailFromDatabase();
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			prgLoading.setVisibility(8);
			sclDetail.setVisibility(0);
			showDetail();
			dbhelper.close();
		}
	}

	/**
	 * this code is used to get data from database and store them to attributes
	 */
	public void getDetailFromDatabase() {
		ArrayList<Object> row = dbhelper.getDetail(id);

		if (row.get(0) != null)
			RecipeName = row.get(0).toString();

		if (row.get(1) != null)
			Preview_1 = row.get(1).toString();
		if (row.get(2) != null)
			Preview_2 = row.get(2).toString();

		if (row.get(3) != null)
			Preview_3 = row.get(3).toString();

		imageList = new String[] { Preview_1, Preview_2, Preview_3 };

		if (row.get(4) != null)
			PrepTime = row.get(4).toString();
		if (row.get(5) != null)
			CookTime = row.get(5).toString();
		if (row.get(6) != null)
			Serves = row.get(6).toString();
		if (row.get(7) != null)
			Summary = row.get(7).toString();
		if (row.get(8) != null)
			Ingredients = row.get(8).toString();
		if (row.get(9) != null)
			Directions = row.get(9).toString();
	}

	/**
	 * then set those values of attributes to the views
	 */
	public void showDetail() {
		txtRecipeName.setText(RecipeName);
		Bitmap bmpDefaultPic;

		String path = SDPATH + "/db_images/" + Preview_1;

		bmpDefaultPic = BitmapFactory.decodeFile(path + ".jpg", null);

		// int imagePreview = getResources().getIdentifier(Preview_1,
		// "drawable",
		// getPackageName());
		// imgPreviewDetail.setImageResource(imagePreview);

		if (bmpDefaultPic == null) {
			imgPreviewDetail.setImageResource(R.drawable.image_preview);
		} else {

			imgPreviewDetail.setImageBitmap(bmpDefaultPic);
		}

		txtPrepTime.setText("prepare : " + PrepTime);
		txtCookTime.setText("action : " + CookTime);
		txtServes.setText("Serves : " + Serves);
		txtSummary.setText(Html.fromHtml(Summary));
		txtIngredients.setText(Html.fromHtml(Ingredients));
		txtDirections.setText(Html.fromHtml(Directions));

		createImageDialog();

		imgPreviewDetail.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				mPopupWindow.showAsDropDown(txtRecipeName);
			}

		});
	}

	public void createImageDialog() {
		View view = View.inflate(this, R.layout.image_dialog, null);

		mPopupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);

		// ColorDrawable dw = new ColorDrawable(color.transparent);
		ColorDrawable dw = new ColorDrawable(0xb0000000);
		//ColorDrawable dw = new ColorDrawable(Color.GRAY);
		mPopupWindow.setBackgroundDrawable(dw);

		Gallery gallery = (Gallery) view.findViewById(R.id.gallery);
		gallery.setSpacing(10);
		gallery.setAdapter(new ImageAdapter(this));

		Button iv = (Button) view.findViewById(R.id.gallery_back);
		iv.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				mPopupWindow.dismiss();
			}

		});

		// TextView tvTitle = (TextView) view.findViewById(R.id.image_title);
		// tvTitle.setText(RecipeName);

		mPopupWindow.setOutsideTouchable(true);
	}

	@Override
	public void onConfigurationChanged(final Configuration newConfig) {
		// Ignore orientation change to keep activity from restarting
		super.onConfigurationChanged(newConfig);
	}

	public class ImageAdapter extends BaseAdapter {
		private Context mContext;

		public ImageAdapter(Context context) {
			mContext = context;
		}

		public int getCount() {
			return imageList.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			Bitmap bmpDefaultPic;
			String path = SDPATH + "/db_images/" + imageList[position];
			bmpDefaultPic = BitmapFactory.decodeFile(path + ".jpg", null);

			ImageView image = new ImageView(mContext);

			if (bmpDefaultPic == null) {
				image.setImageResource(R.drawable.image_preview);
			} else {

				image.setImageBitmap(bmpDefaultPic);
			}
			// image.setImageResource(getResources().getIdentifier(
			// imageList[position], "drawable", getPackageName()));
			image.setAdjustViewBounds(true);
			image.setLayoutParams(new Gallery.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			return image;
		}
	}
}
